package main

import (
	"fmt"
	jieba "github.com/yanyiwu/gojieba"
	"sync"
)

// Resume 简历结构体类型
type Resume struct {
	UserID            int    //简历ID
	Position, Company string //职位、公司
}

// resumeCollection 所有简历
var resumeCollection = []Resume{
	{1, "算法工程师", "腾讯"},
	{2, "算法工程师", "阿里"},
	{3, "后端工程师", "腾讯"},
}

// InvertedIndex 存储倒排索引
var InvertedIndex sync.Map

// generatorInvertedIndex 用于生成的倒排索引
// {1, "算法工程师", "腾讯"},
// {2, "算法工程师", "阿里"},
// {3, "后端工程师", "腾讯"},
// ===>
//算法-->[1 2]
//工程师-->[1 2 3]
//腾讯-->[1 3]
//阿里-->[2]
//后端-->[3]

func generatorInvertedIndex() {
	segger := jieba.NewJieba() //分词器
	defer segger.Free()

	for _, resume := range resumeCollection {
		positionSlice := segger.Cut(resume.Position, true)
		words := append(positionSlice, resume.Company)

		for _, word := range words {
			if value, ok := InvertedIndex.Load(word); !ok {
				InvertedIndex.Store(word, []int{resume.UserID})

			} else {
				InvertedIndex.Store(word, append(value.([]int), resume.UserID)) //value是any类型，这里需要断言为[]int,才能使用append函数
			}
		}

	}

}

func main() {
	generatorInvertedIndex()
	InvertedIndex.Range(func(key, value any) bool {
		fmt.Printf("%v-->%v\n", key, value)
		return true
	})
}
